package com.young.quartz.controller;


import com.young.quartz.domain.dto.AddJobDto;
import com.young.quartz.domain.dto.JobOpertationDto;
import com.young.quartz.domain.dto.JobSearchDto;
import com.young.quartz.domain.dto.QuartzLogSearchDto;
import com.young.quartz.domain.vo.CommonResponse;
import com.young.quartz.service.JobService;
import com.young.quartz.service.QrtzLogService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@Slf4j
@CrossOrigin("*")
@RestController
@RequestMapping(value = "/job")
public class JobController {

    @Autowired
    @Qualifier("Scheduler")
    private Scheduler scheduler;

    @Autowired
    private JobService jobService;

    @Autowired
    private QrtzLogService qrtzLogService;

    /**
     * 获取定时任务列表
     *
     * @param jobSearchDto
     * @return
     */
    @PostMapping("list")
    public CommonResponse queryAll(@RequestBody JobSearchDto jobSearchDto) {
        Map<String, Object> result = jobService.getJobInfoList(jobSearchDto);
        return CommonResponse.success(result);
    }

    /**
     * 添加定时任务
     *
     * @param dto
     * @return
     */
    @PostMapping(value = "add")
    public Object addJob(@RequestBody AddJobDto dto) {
        try {
            return jobService.addJob(dto);
        } catch (Exception e) {
            return CommonResponse.error("系统错误，请到控制台查看具体报错信息");
        }
    }

    /**
     * 修改定时任务信息
     *
     * @param addJobDto
     * @return
     */
    @PostMapping("/update")
    public Object update(@RequestBody AddJobDto addJobDto) throws Exception {
        jobService.updateJob(addJobDto);
        return CommonResponse.success();
    }

    /**
     * 暂停定时任务
     *
     * @param dto
     * @throws Exception
     */
    @PostMapping(value = "/pause")
    public void pause(@RequestBody JobOpertationDto dto) throws Exception {
        jobService.pauseJob(dto);
    }

    /**
     * 重新开始执行定时任务
     *
     * @param dto
     * @throws Exception
     */
    @PostMapping(value = "/resume")
    public void resume(@RequestBody JobOpertationDto dto) throws Exception {
        jobService.resumeJob(dto);
    }

    /**
     * 删除定时任务
     *
     * @param dto
     * @throws Exception
     */
    @PostMapping(value = "/delete")
    public void delete(@RequestBody JobOpertationDto dto) throws Exception {
        jobService.deleteJob(dto);
    }

    /**
     * 触发器
     *
     * @return
     */
    @PostMapping("/trigger")
    public Object trigger(@RequestBody JobOpertationDto dto) {
        jobService.tiggerJob(dto);
        return CommonResponse.success();
    }

    @PostMapping("/log/list")
    public CommonResponse getQuartzLogList(@RequestBody QuartzLogSearchDto dto) {
        Map<String, Object> result = qrtzLogService.getQuartzLogList(dto);
        return CommonResponse.success(result);
    }

    @PostMapping("retry/{logId}")
    public CommonResponse retryJob(@PathVariable("logId") Integer logId) {
        qrtzLogService.retry(logId);
        return CommonResponse.success();
    }




}
